const fs = require('fs') //创建文件、文件夹
const cheerio = require('cheerio') //cheerio爬虫
const iconv = require('iconv-lite') // 编码
const requests = require('sync-request') //node的网络请求
const request = require('request') //利用request模块保存图片

var html = '';
let count = 0 // 记录扒取的图片数量
let imgDirName = '' // 图片存放的目录

let baseUrl = "http://www.ibiquge.cc"
let url = baseUrl + "/ddddfdffd.php?ie=gbk&q=" + encodeURI('满唐华彩'); // 目标网站
// let url = "https://www.baidu.com";

// 拿到网站内容转化成html字符串
request({
    url: url,
    method: "GET",
    encoding: null//获取的内容不编码，二进制
}, function (error, response, buffer) {
    html = iconv.decode(buffer, 'GBK').toString();
    // console.log(html)
    // 调用自己的方法
    search(html)
});

function search(html) {
    if (html) {
        const $ = cheerio.load(html, { decodeEntities: false });
        // console.log($('body .wrap .so_list .type_show .bookbox'))
        $('body .wrap .so_list .type_show .bookbox').each(function (index, elem) {
            console.log($(elem).find('.bookname').text() + '-' + $(elem).find('.author').text())
            let chapterListUrl = baseUrl + $(elem).find('.bookname a').attr('href')
            let bookName = $(elem).find('.bookname').text()
            console.log("网址：" + chapterListUrl)

            fs.exists(bookName, function (exists) {
                if (exists) {
                    console.log("该文件存在！");
                }
                else {
                    // 创建文件夹
                    fs.mkdir(bookName, err => {
                        if (err) {
                            console.log('创建文件夹失败', err);
                            return;
                        }
                        console.log('创建文件夹成功');
                    })
                }
            });

            // 进入章节目录
            request({
                url: chapterListUrl,
                method: "GET",
                encoding: null//获取的内容不编码，二进制
            }, function (error, response, buffer) {
                html = iconv.decode(buffer, 'GBK').toString();
                // console.log(html)
                // 调用自己的方法
                searchChapter(bookName, html)
            });
        })
    }
}

function searchChapter(bookName, html) {
    if (html) {
        const $ = cheerio.load(html, { decodeEntities: false });
        $('body .listmain dd').each(function (index, elem) {
            console.log($(elem).text())
            let chapterUrl = baseUrl + $(elem).find('a').attr('href')
            console.log("网址：" + chapterUrl)

            // 进入章节
            request({
                url: chapterUrl,
                method: "GET",
                encoding: null//获取的内容不编码，二进制
            }, function (error, response, buffer) {
                html = iconv.decode(buffer, 'GBK').toString();
                // console.log(html)
                getContent(bookName, html)
            });
        })
    }
}

function getContent(bookName, html) {
    if (html) {
        const $ = cheerio.load(html, { decodeEntities: false });
        let fileName = bookName + "/" + $('.content h1').text() + '.txt'
        let content = $('#content').text()
        fs.writeFile(fileName, content, (err) => {
            if (err) throw err;
            console.log('文本文件已创建并写入数据');
        });
    }
}